package com.example.windowdemo.window.single;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;

/**
 * @author: njb
 * @date: 2023/8/1 0:00
 * @desc:
 */
public class WindowSingle {
    private Context context;
    private boolean isFullScreen;
    private boolean hasTitleBar;
    private int width;
    private int height;
    private WindowManager.LayoutParams layoutParams;
    private View contentView;
    private static volatile WindowSingle sInstance;

    private WindowSingle(Context context) {
        this.context = context;
        this.layoutParams = new WindowManager.LayoutParams();
    }

    public static WindowSingle getInstance(Context context){
        if (sInstance == null) {
            synchronized (WindowSingle.class) {
                if (sInstance == null) {
                    sInstance = new WindowSingle(context);
                }
            }
        }
        return sInstance;
    }

    public void setFullScreen(boolean isFullScreen) {
        this.isFullScreen = isFullScreen;
        if (isFullScreen) {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setTitleBar(boolean hasTitleBar) {
        this.hasTitleBar = hasTitleBar;
        if (hasTitleBar) {
            layoutParams.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
        } else {
            layoutParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        }
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        layoutParams.width = width;
        layoutParams.height = height;
    }

    public void setPosition(int x, int y) {
        layoutParams.x = x;
        layoutParams.y = y;
    }

    public void setGravity(int gravity) {
        layoutParams.gravity = gravity;
    }

    public void addView(View view) {
        this.contentView = view;
    }

    public void show() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.addView(contentView, layoutParams);
    }

    public void hide() {
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.removeView(contentView);
    }

}
